// Do file that creates indicators of inter-country marriage and work patterns.
//
// Last changed: 2020-06-04 (KOL)


  use D:\SCB_ConPol\Stata\RTB\Fodelseuppg, clear
  bysort LopNr: egen cID = count(LopNr)
  keep if cID == 1
  drop cID 
  
  tempfile FodUppg
  save `FodUppg'
  
foreach yr in 1991 1994 1998 2002 2006 2010 2014 {
  
  use LopNr FamStF FamTypF SyssStat* AstNr AstKom cfarlopnr FamId using "D:\SCB_ConPol\Stata\LISA\LISA_`yr'", clear
  gen Ar = `yr'
  bysort LopNr: egen cID = count(LopNr)
  keep if cID == 1
  drop cID
  
  replace cfarlopnr = . if SyssStat != 1
  replace cfarlopnr = . if cfarlopnr == 598162
  tempfile Lisa`yr'
  save `Lisa`yr''
} 

use `Lisa1991'
foreach yr in 1994 1998 2002 2006 2010 2014 {
 
  append using `Lisa`yr''
} 

  replace FamStF = FamStF*10 if FamStF<100
	
  gen ManPar  = inlist(FamStF, 110, 130)
  gen KvinPar = inlist(FamStF, 120, 140) 

  merge m:1 LopNr using `FodUppg'
  keep if _merge == 3
  drop _merge
  
  merge m:1 LopNr using "D:\SCB_ConPol\Stata\RTB\FodelselandIFAUGrupp.dta"
  keep if _merge == 3
  
  gen Infodd = IFAUkod == 0
  bysort cfarlopnr Ar: egen AntArb = count(LopNr)
  bysort cfarlopnr Ar: egen AntArbInfodd = total(Infodd)
  bysort cfarlopnr IFAUkod Ar: egen AntArbFland = count(LopNr)
  replace AntArb = . if cfarlopnr == .
  replace AntArbInfodd = . if cfarlopnr == .
  replace AntArbFland = . if cfarlopnr == .
  
  gen LandMan = IFAUkod if ManPar == 1
  bysort FamId: egen mLandMan = min(LandMan)
  drop LandMan
  rename mLandMan LandMan
	
  gen LandKvinna = IFAUkod if KvinPar == 1
  bysort FamId: egen mLandKvin = min(LandKvinna)
  drop LandKvin
  rename mLandKvin LandKvin

  gen PartFland = LandKvin if ManPar == 1
  replace PartFland = LandMan if KvinPar == 1

  keep LopNr Ar AntArb AntArbInfodd AntArbFland PartFland FamTypF
  
  merge 1:1 LopNr Ar using "E:\ProjData\UtplaceringsData8594\utp-UtpData"
  keep if _merge == 3
  drop _merge
  
  keep LopNr Ar AntArb AntArbInfodd AntArbFland PartFland FamTypF
  
  gen PartSvensk = PartFland==0
  gen ArbAndSvensk = AntArbInfodd/AntArb
  
  save "E:\ProjData\UtplaceringsData8594\utp-EthnicContacts.dta", replace
